QuickOPC User's Guide and Reference
Examples - OPC Unified Architecture - Call a single method

.NET

// This example shows how to call a single method, and pass arguments to and from it.

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples._EasyUAClient
{
    class CallMethod
    {
        public static void Main1()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            object[] inputs =
            {
                false, 
                1, 
                2, 
                3, 
                4, 
                5, 
                6, 
                7, 
                8, 
                9, 
                10
            };

            TypeCode[] typeCodes =
            {
                TypeCode.Boolean,
                TypeCode.SByte,
                TypeCode.Byte,
                TypeCode.Int16,
                TypeCode.UInt16,
                TypeCode.Int32,
                TypeCode.UInt32,
                TypeCode.Int64,
                TypeCode.UInt64,
                TypeCode.Single,
                TypeCode.Double
            };

            // Instantiate the client object
            var client = new EasyUAClient();

            // Perform the operation.
            object[] outputs;
            try
            {
                outputs = client.CallMethod(
                    endpointDescriptor,
                    "nsu=http://test.org/UA/Data/ ;i=10755",
                    "nsu=http://test.org/UA/Data/ ;i=10756",
                    inputs,
                    typeCodes);
            }
            catch (UAException uaException)
            {
                Console.WriteLine($"*** Failure: {uaException.GetBaseException().Message}");
                return;
            }

            // Display results
            for (int i = 0; i < outputs.Length; i++)
                Console.WriteLine($"outputs[{i}]: {outputs[i]}");

            // Example output:
            //outputs[0]: False
            //outputs[1]: 1
            //outputs[2]: 2
            //outputs[3]: 3
            //outputs[4]: 4
            //outputs[5]: 5
            //outputs[6]: 6
            //outputs[7]: 7
            //outputs[8]: 8
            //outputs[9]: 9
            //outputs[10]: 10        
        }
    }
}
# This example shows how to call a single method, and pass arguments to and from it.

#requires -Version 5.1
using namespace System
using namespace OpcLabs.EasyOpc.UA
using namespace OpcLabs.EasyOpc.UA.OperationModel

# The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows .
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUA.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUAComponents.dll"

[UAEndpointDescriptor]$endpointDescriptor =
    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
# or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
# or "https://opcua.demo-this.com:51212/UA/SampleServer/"

$inputs = @(
    $false,
    1, 
    2, 
    3, 
    4, 
    5, 
    6, 
    7, 
    8, 
    9, 
    10)

$typeCodes = @(
    [TypeCode]::Boolean,
    [TypeCode]::SByte,
    [TypeCode]::Byte,
    [TypeCode]::Int16,
    [TypeCode]::UInt16,
    [TypeCode]::Int32,
    [TypeCode]::UInt32,
    [TypeCode]::Int64,
    [TypeCode]::UInt64,
    [TypeCode]::Single,
    [TypeCode]::Double)

# Instantiate the client object.
$client = New-Object EasyUAClient

# Perform the operation.
try {
    $outputs = $client.CallMethod(
                    $endpointDescriptor,
                    "nsu=http://test.org/UA/Data/ ;i=10755",
                    "nsu=http://test.org/UA/Data/ ;i=10756",
                    $inputs,
                    $typeCodes)
}
catch [UAException] {
    Write-Host "*** Failure: $($PSItem.Exception.GetBaseException().Message)"
    return
}

# Display results
for ($i = 0; $i -lt $outputs.Length; $i++) {
    Write-Host "outputs[$($i)]: $($outputs[$i])"
}


# Example output:
#
#outputs[0]: False
#outputs[1]: 1
#outputs[2]: 2
#outputs[3]: 3
#outputs[4]: 4
#outputs[5]: 5
#outputs[6]: 6
#outputs[7]: 7
#outputs[8]: 8
#outputs[9]: 9
#outputs[10]: 10        

# This example shows how to call a single method, and pass arguments to and from it.

# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.OperationModel import *


endpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer')
# or 'http://opcua.demo-this.com:51211/UA/SampleServer' (currently not supported)
# or 'https://opcua.demo-this.com:51212/UA/SampleServer/'

# 
inputs = [False, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
typeCodes = [
    TypeCode.Boolean,
    TypeCode.SByte,
    TypeCode.Byte,
    TypeCode.Int16,
    TypeCode.UInt16,
    TypeCode.Int32,
    TypeCode.UInt32,
    TypeCode.Int64,
    TypeCode.UInt64,
    TypeCode.Single,
    TypeCode.Double,
    ]

# Instantiate the client object.
client = EasyUAClient()

# Perform the operation.
try:
    outputs = IEasyUAClientExtension.CallMethod(client,
        endpointDescriptor,
        UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10755'),
        UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10756'),
        inputs,
        typeCodes)
except UAException as uaException:
    print('*** Failure: ' + uaException.GetBaseException().Message)
    exit()

# Display results.
for i, value in enumerate(outputs):
     print('outputs[', i, ']: ', value, sep='')


# Example output:
#outputs[0]: False
#outputs[1]: 1
#outputs[2]: 2
#outputs[3]: 3
#outputs[4]: 4
#outputs[5]: 5
#outputs[6]: 6
#outputs[7]: 7
#outputs[8]: 8
#outputs[9]: 9.0
#outputs[10]: 10.0

' This example shows how to call a single method, and pass arguments to and from it.

Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.OperationModel

Namespace _EasyUAClient
    Friend Class CallMethod
        Public Shared Sub Main1()

            Dim endpointDescriptor As UAEndpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
            ' or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            ' or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            Dim inputs() As Object =
            { _
                False,
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9,
                10
            }

            Dim typeCodes() As TypeCode =
            { _
                TypeCode.Boolean,
                TypeCode.SByte,
                TypeCode.Byte,
                TypeCode.Int16,
                TypeCode.UInt16,
                TypeCode.Int32,
                TypeCode.UInt32,
                TypeCode.Int64,
                TypeCode.UInt64,
                TypeCode.Single,
                TypeCode.Double
            }

            ' Instantiate the client object
            Dim client = New EasyUAClient()

            ' Perform the operation
            Dim outputs() As Object
            Try
                outputs = client.CallMethod( _
                    endpointDescriptor, _
                    "nsu=http://test.org/UA/Data/ ;i=10755", _
                    "nsu=http://test.org/UA/Data/ ;i=10756", _
                    inputs, _
                    typeCodes)
            Catch uaException As UAException
                Console.WriteLine("*** Failure: {0}", uaException.GetBaseException.Message)
                Exit Sub
            End Try

            ' Display results
            For i As Integer = 0 To outputs.Length - 1
                Console.WriteLine("outputs[{0}]: {1}", i, outputs(i))
            Next i

            ' Example output:
            'outputs[0]: False
            'outputs[1]: 1
            'outputs[2]: 2
            'outputs[3]: 3
            'outputs[4]: 4
            'outputs[5]: 5
            'outputs[6]: 6
            'outputs[7]: 7
            'outputs[8]: 8
            'outputs[9]: 9
            'outputs[10]: 10        
        End Sub
    End Class
End Namespace

COM

// This example shows how to call a single method, and pass arguments to and from it.

#include "stdafx.h"    // Includes "QuickOpc.h", and other commonly used files
#include <atlsafe.h>
#include "CallMethod.h"

namespace _EasyUAClient
{
    void CallMethod::Main()
    {
        // Initialize the COM library
        CoInitializeEx(NULL, COINIT_MULTITHREADED);
        {
            CComSafeArray<VARIANT> inputs(11);
            inputs.SetAt(0, _variant_t(false));
            inputs.SetAt(1, _variant_t(1));
            inputs.SetAt(2, _variant_t(2));
            inputs.SetAt(3, _variant_t(3));
            inputs.SetAt(4, _variant_t(4));
            inputs.SetAt(5, _variant_t(5));
            inputs.SetAt(6, _variant_t(6));
            inputs.SetAt(7, _variant_t(7));
            inputs.SetAt(8, _variant_t(8));
            inputs.SetAt(9, _variant_t(9));
            inputs.SetAt(10, _variant_t(10));
            CComVariant vInputs(inputs);

            CComSafeArray<VARIANT> typeCodes(11);
            typeCodes.SetAt(0, _variant_t(TypeCode_Boolean));
            typeCodes.SetAt(1, _variant_t(TypeCode_SByte));
            typeCodes.SetAt(2, _variant_t(TypeCode_Byte));
            typeCodes.SetAt(3, _variant_t(TypeCode_Int16));
            typeCodes.SetAt(4, _variant_t(TypeCode_UInt16));
            typeCodes.SetAt(5, _variant_t(TypeCode_Int32));
            typeCodes.SetAt(6, _variant_t(TypeCode_UInt32));
            typeCodes.SetAt(7, _variant_t(TypeCode_Int64));
            typeCodes.SetAt(8, _variant_t(TypeCode_UInt64));
            typeCodes.SetAt(9, _variant_t(TypeCode_Single));
            typeCodes.SetAt(10, _variant_t(TypeCode_Double));
            CComVariant vTypeCodes(typeCodes);

            // Instantiate the client object
            _EasyUAClientPtr ClientPtr(__uuidof(EasyUAClient));

            // Perform the operation
            CComSafeArray<VARIANT> outputs;
            outputs.Attach(ClientPtr->CallMethod(
                //L"http://opcua.demo-this.com:51211/UA/SampleServer",
                L"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer",
                L"nsu=http://test.org/UA/Data/ ;i=10755",
                L"nsu=http://test.org/UA/Data/ ;i=10756",
                &vInputs,
                &vTypeCodes));
    
            // Display results
            for (int i = outputs.GetLowerBound(); i <= outputs.GetUpperBound(); i++)
            {
                _variant_t output(outputs[i]);
                _variant_t vString;
                vString.ChangeType(VT_BSTR, &output);
                _tprintf(_T("outputs(d)s\n"), i, (LPCTSTR)CW2CT((_bstr_t)vString));
            }
        }
         // Release all interface pointers BEFORE calling CoUninitialize()
        CoUninitialize();
    }
}
// This example shows how to call a single method, and pass arguments to and
// from it.

class procedure CallMethod.Main;
var
  Client: EasyUAClient;
  I: Cardinal;
  Inputs: OleVariant;
  Outputs: OleVariant;
  TypeCodes: OleVariant;
begin
  Inputs := VarArrayCreate([0, 10], varVariant);
  Inputs[0] := False;
  Inputs[1] := 1;
  Inputs[2] := 2;
  Inputs[3] := 3;
  Inputs[4] := 4;
  Inputs[5] := 5;
  Inputs[6] := 6;
  Inputs[7] := 7;
  Inputs[8] := 8;
  Inputs[9] := 9;
  Inputs[10] := 10;

  TypeCodes := VarArrayCreate([0, 10], varVariant);
  TypeCodes[0] := TypeCode_Boolean;
  TypeCodes[1] := TypeCode_SByte;
  TypeCodes[2] := TypeCode_Byte;
  TypeCodes[3] := TypeCode_Int16;
  TypeCodes[4] := TypeCode_UInt16;
  TypeCodes[5] := TypeCode_Int32;
  TypeCodes[6] := TypeCode_UInt32;
  TypeCodes[7] := TypeCode_Int64;
  TypeCodes[8] := TypeCode_UInt64;
  TypeCodes[9] := TypeCode_Single;
  TypeCodes[10] := TypeCode_Double;

  // Instantiate the client object
  Client := CoEasyUAClient.Create;

  // Perform the operation
  TVarData(Outputs).VType := varArray or varVariant;
  TVarData(Outputs).VArray := PVarArray(Client.CallMethod(
    //'http://opcua.demo-this.com:51211/UA/SampleServer',
    'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
    'nsu=http://test.org/UA/Data/ ;i=10755',
    'nsu=http://test.org/UA/Data/ ;i=10756',
    PSafeArray(TVarData(Inputs).VArray),
    PSafeArray(TVarData(TypeCodes).VArray)));

  // Display results
  for I := VarArrayLowBound(Outputs, 1) to VarArrayHighBound(Outputs, 1) do
      try
        WriteLn('outputs(', I, '): ', Outputs[I]);
      except
        on EVariantError do WriteLn('*** Error displaying the value');
      end;
end;
// This example shows how to call a single method, and pass arguments to and
// from it.

class procedure CallMethod.Main;
var
  Client: OpcLabs_EasyOpcUA_TLB._EasyUAClient;
  I: Cardinal;
  Inputs: OleVariant;
  Outputs: OleVariant;
  TypeCodes: OleVariant;
begin
  Inputs := VarArrayCreate([0, 10], varVariant);
  Inputs[0] := False;
  Inputs[1] := 1;
  Inputs[2] := 2;
  Inputs[3] := 3;
  Inputs[4] := 4;
  Inputs[5] := 5;
  Inputs[6] := 6;
  Inputs[7] := 7;
  Inputs[8] := 8;
  Inputs[9] := 9;
  Inputs[10] := 10;

  TypeCodes := VarArrayCreate([0, 10], varVariant);
  TypeCodes[0] := TypeCode_Boolean;
  TypeCodes[1] := TypeCode_SByte;
  TypeCodes[2] := TypeCode_Byte;
  TypeCodes[3] := TypeCode_Int16;
  TypeCodes[4] := TypeCode_UInt16;
  TypeCodes[5] := TypeCode_Int32;
  TypeCodes[6] := TypeCode_UInt32;
  TypeCodes[7] := TypeCode_Int64;
  TypeCodes[8] := TypeCode_UInt64;
  TypeCodes[9] := TypeCode_Single;
  TypeCodes[10] := TypeCode_Double;

  // Instantiate the client object
  Client := CoEasyUAClient.Create;

  // Perform the operation
  try
    TVarData(Outputs).VType := varArray or varVariant;
    TVarData(Outputs).VArray := PVarArray(Client.CallMethod(
      //'http://opcua.demo-this.com:51211/UA/SampleServer',
      //'https://opcua.demo-this.com:51212/UA/SampleServer/',
      'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
      'nsu=http://test.org/UA/Data/ ;i=10755',
      'nsu=http://test.org/UA/Data/ ;i=10756',
      Inputs,
      TypeCodes));
  except
    on E: EOleException do
    begin
      WriteLn(Format('*** Failure: %s', [E.GetBaseException.Message]));
      Exit;
    end;
  end;

  // Display results
  for I := VarArrayLowBound(Outputs, 1) to VarArrayHighBound(Outputs, 1) do
      WriteLn('outputs(', I, '): ', Outputs[I]);

   VarClear(Outputs);
   VarClear(TypeCodes);
   VarClear(Inputs);

end;
// This example shows how to call a single method, and pass arguments to and from it.

$inputs[0] = false;
$inputs[1] = 1;
$inputs[2] = 2;
$inputs[3] = 3;
$inputs[4] = 4;
$inputs[5] = 5;
$inputs[6] = 6;
$inputs[7] = 7;
$inputs[8] = 8;
$inputs[9] = 9;
$inputs[10] = 10;

$typeCodes[0] = 3;    // TypeCode.Boolean
$typeCodes[1] = 5;    // TypeCode.SByte
$typeCodes[2] = 6;    // TypeCode.Byte
$typeCodes[3] = 7;    // TypeCode.Int16
$typeCodes[4] = 8;    // TypeCode.UInt16
$typeCodes[5] = 9;    // TypeCode.Int32
$typeCodes[6] = 10;   // TypeCode.UInt32
$typeCodes[7] = 11;   // TypeCode.Int64
$typeCodes[8] = 12;   // TypeCode.UInt64
$typeCodes[9] = 13;   // TypeCode.Single
$typeCodes[10] = 14;  // TypeCode.Double

// Instantiate the client object
$Client = new COM("OpcLabs.EasyOpc.UA.EasyUAClient");

// Perform the operation
try
{
    $outputs = $Client->CallMethod(
        //"http://opcua.demo-this.com:51211/UA/SampleServer", 
        "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", 
        "nsu=http://test.org/UA/Data/ ;i=10755", 
        "nsu=http://test.org/UA/Data/ ;i=10756", 
        $inputs, 
        $typeCodes);
}
catch (com_exception $e)
{
    printf("*** Failure: %s\n", $e->getMessage());
    Exit();
}

// Display results
for ($i = 0; $i < count($outputs); $i++)
{
    printf("outputs[d]s\n", $i, $outputs[$i]);
}
# This example shows how to call a single method, and pass arguments to and from it.

# The Python for Windows (pywin32) extensions package is needed. Install it using "pip install pypiwin32".
# CAUTION: We now recommend using Python.NET package instead. Full set of examples with Python.NET is available!
import win32com.client
from pywintypes import com_error

# Instantiate the client object
client = win32com.client.Dispatch('OpcLabs.EasyOpc.UA.EasyUAClient') 

# 
inputs = [False, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
typeCodes = [
    3,  # TypeCode.Boolean
    5,  # TypeCode.SByte
    6,  # TypeCode.Byte
    7,  # TypeCode.Int16
    8,  # TypeCode.UInt16
    9,  # TypeCode.Int32
    10, # TypeCode.UInt32
    11, # TypeCode.Int64
    12, # TypeCode.UInt64
    13, # TypeCode.Single
    14  # TypeCode.Double
    ]


# Perform the operation
try:
    outputs = client.CallMethod(
        'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
        'nsu=http://test.org/UA/Data/ ;i=10755',
        'nsu=http://test.org/UA/Data/ ;i=10756',
        inputs,
        typeCodes)
    # outputs[0] contains the actual results; outputs[1] and outputs[2] are copies of inputs and typeCodes.
except com_error as e:
    print('*** Failure: ' + e.args[2][1] + ': ' + e.args[2][2])
    exit()


# Display results
for i, value in enumerate(outputs[0]):
     print('outputs[', i, ']: ', value)

# Example output:
#outputs[0]: False
#outputs[1]: 1
#outputs[2]: 2
#outputs[3]: 3
#outputs[4]: 4
#outputs[5]: 5
#outputs[6]: 6
#outputs[7]: 7
#outputs[8]: 8
#outputs[9]: 9.0
#outputs[10]: 10.0

Rem This example shows how to call a single method, and pass arguments to and from it.

Public Sub CallMethod_Main_Command_Click()
    OutputText = ""

    Dim inputs(10)
    inputs(0) = False
    inputs(1) = 1
    inputs(2) = 2
    inputs(3) = 3
    inputs(4) = 4
    inputs(5) = 5
    inputs(6) = 6
    inputs(7) = 7
    inputs(8) = 8
    inputs(9) = 9
    inputs(10) = 10

    Dim typeCodes(10)
    typeCodes(0) = 3    ' TypeCode.Boolean
    typeCodes(1) = 5    ' TypeCode.SByte
    typeCodes(2) = 6    ' TypeCode.Byte
    typeCodes(3) = 7    ' TypeCode.Int16
    typeCodes(4) = 8    ' TypeCode.UInt16
    typeCodes(5) = 9    ' TypeCode.Int32
    typeCodes(6) = 10   ' TypeCode.UInt32
    typeCodes(7) = 11   ' TypeCode.Int64
    typeCodes(8) = 12   ' TypeCode.UInt64
    typeCodes(9) = 13   ' TypeCode.Single
    typeCodes(10) = 14  ' TypeCode.Double

    ' Instantiate the client object
    Dim Client As New EasyUAClient

    ' Perform the operation
    On Error Resume Next
    Dim outputs As Variant
    outputs = Client.CallMethod( _
        "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", _
        "nsu=http://test.org/UA/Data/ ;i=10755", _
        "nsu=http://test.org/UA/Data/ ;i=10756", _
        inputs, _
        typeCodes)
    If Err.Number <> 0 Then
        OutputText = OutputText & "*** Failure: " & Err.Source & ": " & Err.Description & vbCrLf
        Exit Sub
    End If
    On Error GoTo 0

    ' Display results
    Dim i: For i = LBound(outputs) To UBound(outputs)
        On Error Resume Next
        OutputText = OutputText & "outputs(" & i & "): " & outputs(i) & vbCrLf
        If Err <> 0 Then OutputText = OutputText & "*** Error" & vbCrLf ' occurrs with types not recognized by VB6
        On Error GoTo 0
    Next
End Sub
Rem This example shows how to call a single method, and pass arguments to and from it.

Option Explicit

Dim inputs(10)
inputs(0) = False
inputs(1) = 1
inputs(2) = 2
inputs(3) = 3
inputs(4) = 4
inputs(5) = 5
inputs(6) = 6
inputs(7) = 7
inputs(8) = 8
inputs(9) = 9
inputs(10) = 10

Dim typeCodes(10)
typeCodes(0) = 3    ' TypeCode.Boolean
typeCodes(1) = 5    ' TypeCode.SByte
typeCodes(2) = 6    ' TypeCode.Byte
typeCodes(3) = 7    ' TypeCode.Int16
typeCodes(4) = 8    ' TypeCode.UInt16
typeCodes(5) = 9    ' TypeCode.Int32
typeCodes(6) = 10   ' TypeCode.UInt32
typeCodes(7) = 11   ' TypeCode.Int64
typeCodes(8) = 12   ' TypeCode.UInt64
typeCodes(9) = 13   ' TypeCode.Single
typeCodes(10) = 14  ' TypeCode.Double

' Instantiate the client object
Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.UA.EasyUAClient")

' Perform the operation
On Error Resume Next
Dim outputs: outputs = Client.CallMethod( _
    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", _
    "nsu=http://test.org/UA/Data/ ;i=10755", _
    "nsu=http://test.org/UA/Data/ ;i=10756", _
    inputs, _
    typeCodes)
If Err.Number <> 0 Then
    WScript.Echo "*** Failure: " & Err.Source & ": " & Err.Description
    WScript.Quit
End If
On Error Goto 0

' Display results
Dim i: For i = LBound(outputs) To UBound(outputs)
    On Error Resume Next
    WScript.Echo "outputs(" & i & "): " & outputs(i)
    If Err <> 0 Then WScript.Echo "*** Error"   ' occurrs with types not recognized by VBScript
    On Error Goto 0
Next

 

See Also

Conceptual